package com.henry.chatkit.messages

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.henry.chatkit.MessageType
import com.henry.chatkit.R
import com.henry.chatkit.messages.holder.*

object MessageFactory {

    fun getInstance(
            parent: ViewGroup?,
            adapter: RecyclerView.Adapter<*>?,
            viewType: Int
    ): RecyclerView.ViewHolder? {
        val inflater = LayoutInflater.from(parent?.context)
        var holder: RecyclerView.ViewHolder? = null
        var view: View? = null

        // 头部的holder
        if (viewType == MessageType.HEADER_VIEW) {
            view = inflater.inflate(R.layout.message_adapter_content_header, parent, false)
            holder = MessageHeaderHolder(view)
            return holder
        }

        // 加群消息等holder
//            if (viewType == Message.MsgType.tips) {
//                view = inflater.inflate(R.layout.message_adapter_item_empty, parent, false)
//                holder = MessageTipsHolder(view)
//            }

        // 具体消息holder
        view = inflater.inflate(R.layout.message_adapter_item_content, parent, false)

        when (viewType) {
            MessageType.TEXT -> holder = MessageTextHolder(view)
            MessageType.IMAGE -> holder = MessageImageHolder(view)
            MessageType.AUDIO -> holder = MessageAudioHolder(view)
            MessageType.CUSTOM -> holder = MessageCustomHolder(view)
            MessageType.FILE -> holder = MessageFileHolder(view)
            MessageType.SYSTEM -> {
                view = inflater.inflate(R.layout.message_adapter_item_empty, parent, false)
                holder = MessageSystemHolder(view)
            }
        }

        if (holder != null && adapter != null) {
            (holder as MessageEmptyHolder).setAdapter(adapter)
        }
        return holder
    }
}